import { defineStore } from 'pinia'
// @ts-ignore
import {ElLoading,ElMessage} from "element-plus";
import {getCurrentInstance} from "vue";
// @ts-ignore

export default defineStore('storeIdC', {
    // 其他配置...
    state: ()=>{
        return{
            // 原始数据商品列表
            proList:[],
            // 渲染数据
            searchList: []
        }
    },
    // pinia中的getters与vuex中的一摸一样
    getters: {
    },
    // 同步异步统一到actions中了
    actions: {
        // 加载动画
        loading():void {
            const load = ElLoading.service({
                lock: true,
                text: '数据库比对中...',
                background: 'rgba(0, 0, 0, 0.7)',
            })
            setTimeout(():void => {
                load.close()
            }, 1000)
        },
        // 商品列表数据请求
        async http():Promise<void> {
            // @ts-ignore 获取全局的一个对象
            const {proxy} = getCurrentInstance()
            // console.log(proxy.$echarts)
            // 用解构的方式把数据解构出来使用并且赋值给proList、searchList
            // console.log(555, proxy.$axios)
            const {data: {list}} = await proxy.$axios.get('/json/evaluate.json')
            this.proList = list
            this.searchList =list
            console.log(list)
        },
        //  商品数据proList组件中点击search执行筛选方法
        // 搜索功能
        searchA(name:any){
            console.log(name)
            if(name === null || name ===''){
                ElMessage({
                    type: 'warning',
                    message: '还原列表数据'
                })
                this.searchList = this.proList
            }else {
                ElMessage({
                    type: 'success',
                    message: '分类对应的数据展示如下'
                })
                // 筛选出数据赋给searchList
                this.searchList = this.proList.filter((item:any)=>item.name ===name)
            }
        },
        // 商品数据添加数据
        add(obj:any):void {
            const arr = {
                name: obj.name,
                additive: obj.additive,
                time: obj.time,
                key: this.proList.length+1
            }
            const length:number = this.proList.length
            //@ts-ignore
            this.proList.push(arr)
            this.proList = this.proList.reduce((acc, obj:any) => {
                // console.log(obj)
                const existingObj = acc.find((item:any) => JSON.stringify(`${item.name}+${item.additive}`) === JSON.stringify(`${obj.name}+${obj.additive}`));
                console.log(existingObj)
                if (!existingObj) {
                    //@ts-ignore
                    acc.push(obj);
                }
                return acc;
            }, []);
            if(this.proList.length === length){
                ElMessage({
                    message: '类型已经存在，请选则其他类型',
                    type: 'warning'
                })
            }else {
                ElMessage({
                    message: '添加成功',
                    type: 'success'
                })
            }
            this.searchList = this.proList
        },
        // 商品列表删除数据
        filterB(key: number):void{
            console.log(key)
            this.searchList = this.proList.filter((item:any):boolean =>item.key !==key)
            this.proList = this.searchList
            ElMessage({
                type: 'success',
                message: '删除成功'
            })
        },
        // // 商品列表修改数据
        editA(val: any, key: number) {
            console.log(key,val)
            console.log(this.proList)
            this.proList.some((item: any) => {
                if (item.key === key) {
                    item.additive = val.additive
                    item.time = val.time
                    item.name = val.name
                    return true
                }
            })
            ElMessage({
                type: 'success',
                message: '修改成功'
            })
        },
    }
});

export class useStore {
}
